using System;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Collections;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Reflection;

//using WP.Commons;


namespace WP.Controls
{
	/// <summary>
    /// Summary description for ComboBoxEx.
	/// </summary>
	
//    [Designer(typeof(ComboBoxEx.MyTypeDesigner))]
	public class ComboBoxEx : System.Windows.Forms.ComboBox
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion

  		#region " **************** Enumerations and class variables ****************"
	  	bool PressedKey = false;
  	
	  	string wcol; //Column widths
	  	string wcol1;
	  	string wcol2;
	  	string wcol3;
	  	string wcol4;
	 	Color currentColor; //Current border color
  	
	  	Timer myTimer = new Timer();
	  	int arrowWidth = 12;
	  	bool bUsingLargeFont = false;
	  	Color arrowColor = Color.Black;
	  	Color arrowDisableColor = Color.LightGray;
	  	Color customBorderColor = Color.Empty;
	  	Color borderColor = SystemColors.Highlight;
	  	Color dropDownArrowAreaNormalColor = SystemColors.Control;
	  	Color dropDownArrowAreaHotColor = Color.Black;
	  	Color dropDownArrowAreaPressedColor = Color.Black;
	  	bool Highlighted = true;
	  	int[] Indice = new int[5];
	  	bool MouseOver = false;
  	
	  	//constants for CharacterCasing
	  	const int CBS_UPPERCASE = 0x2000;
	  	const int CBS_LOWERCASE = 0x4000;

        //BorderType
        public enum TipiBordi
        {
            Fixed3D,
            FlatXP
        }

        //Loading Type
        public enum CaricamentoCombo
        {
            ComboBoxItem,
            DataTable
        }

        //Our DropDownStyle to manage the DropDownList with Autocomplete
        public enum CustomDropDownStyle
        {
            DropDown,
            DropDownList
        }
  	
	  	//Property Declaration
	  	private System.Windows.Forms.ComboBox mComboBox;
	  	int m_ColumnNum = 1;
	  	string m_ColumnWidth = "0";
	  	Color m_NormalBorderColor = Color.Black;
	  	Color m_DropDownForeColor = Color.Black;
	  	Color m_DropDownBackColor = Color.FromArgb(193, 210, 238);
	  	Color m_DropDownArrowBackColor = Color.FromArgb(136, 169, 223);
	  	bool m_GridLineVertical = false;
	  	bool m_GridLineHorizontal = false;
	  	Color m_GridLineColor = Color.LightGray;
	  	CharacterCasing m_CharacterCasing = CharacterCasing.Normal;
	  	TipiBordi m_BorderStyle = TipiBordi.Fixed3D;
	  	Color m_HighlightBorderColor = Color.Blue;
	  	bool m_HighlightBorderOnMouseEvents = true;
	  	DataTable m_DataTable;
	  	string[] m_SourceDataString;
	  	CaricamentoCombo m_LoadingType = CaricamentoCombo.ComboBoxItem;
  		bool m_ManagingFastMouseMoving = true;
  		int m_ManagingFastMouseMovingInterval = 30;
  		CustomDropDownStyle m_DropDownStyle = CustomDropDownStyle.DropDown;
	  	#endregion
  	
  		#region " **************** Custom Events ****************"
  		public delegate void DrawItemEventHandler(object sender, System.Windows.Forms.DrawItemEventArgs e);
  		private DrawItemEventHandler DrawItemEvent;
	  	
  		public new event DrawItemEventHandler DrawItem
  		{
  			add
  			{
  				DrawItemEvent = (DrawItemEventHandler) System.Delegate.Combine(DrawItemEvent, value);
  			}
  			remove
  			{
  				DrawItemEvent = (DrawItemEventHandler) System.Delegate.Remove(DrawItemEvent, value);
  			}
  		}
	  	
  		#endregion
	  	
  		#region " **************** Constructor and Dispose ****************"
  		//Constructor
  		public ComboBoxEx() {
  			mComboBox = this;
			m_ColumnWidth = this.Width.ToString();
  			mComboBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(mComboBox_DrawItem);
  			myTimer.Tick += new System.EventHandler(TimerEventProcessor);
			InitializeComponent();
  		}
	  	
  		protected override void Dispose (bool disposing)
  		{
  			if (myTimer.Enabled)
  			{
  				myTimer.Stop();
  			}
  			if (disposing)
  			{
  				base.Dispose(true);
  			}
  			GC.SuppressFinalize(this);
  		}
  		#endregion

		#region " **************** Designer Custom ****************"

		internal class MyTypeDesigner : System.Windows.Forms.Design.ControlDesigner
  		{
	  		
	  		
  			private DesignerVerbCollection verbi;
	  		
  			private void OnVerbFlatXP (object sender, EventArgs e)
  			{
  				foreach (DesignerVerb verbo in Verbs)
  				{
  					verbo.Checked = false;
  				}
  				((DesignerVerb) sender).Checked = true;
  				PropertyDescriptor tipoBordo;
  				tipoBordo = TypeDescriptor.GetProperties(Control)["BorderStyle"];
  				tipoBordo.SetValue(Control, TipiBordi.FlatXP);
  			}
	  		
  			private void OnVerbFixed3D (object sender, EventArgs e)
  			{
  				foreach (DesignerVerb verbo in Verbs)
  				{
  					verbo.Checked = false;
  				}
  				((DesignerVerb) sender).Checked = true;
  				PropertyDescriptor tipoBordo;
  				tipoBordo = TypeDescriptor.GetProperties(Control)["BorderStyle"];
  				tipoBordo.SetValue(Control, TipiBordi.Fixed3D);
  			}
	  		
  			private void OnVerbInformazioniSu (object sender, EventArgs e)
  			{
//  				frmAboutComboBox frmAbout = new frmAboutComboBox();
	  			
//  				frmAbout.ShowDialog();
  			}
	  		
  			public override DesignerVerbCollection Verbs
  			{
  				get{
  					if (verbi == null)
  					{
  						verbi = new DesignerVerbCollection();
  						verbi.Add(new DesignerVerb("FlatXP", new EventHandler( OnVerbFlatXP)));
  						verbi.Add(new DesignerVerb("Fixed3D", new EventHandler( OnVerbFixed3D)));
// 						verbi.Add(new DesignerVerb("About ComboBoxEx", new EventHandler( OnVerbInformazioniSu)));
	  					
  						PropertyDescriptor tipoBordo;
  						tipoBordo = TypeDescriptor.GetProperties(Control)["BorderStyle"];
  						TipiBordi mtipoBordo;
	  					
  						mtipoBordo = ((TipiBordi) tipoBordo.GetValue(this.Component));
  						if (mtipoBordo == TipiBordi.FlatXP)
  						{
  							verbi[0].Checked = true;
  						}
  						else if (mtipoBordo == TipiBordi.Fixed3D)
  						{
  							verbi[1].Checked = true;
  						}
	  					
  					}
  					return verbi;
  				}
  			}
	  		
  			protected override void PreFilterProperties (IDictionary properties)
  			{
  				base.PreFilterProperties(properties);
	  			
  				System.Attribute[] attributoVero = new System.Attribute[] { new ReadOnlyAttribute(true) };
  				System.Attribute[] attributoFalso = new System.Attribute[] { new ReadOnlyAttribute(false) };
	  			
  				PropertyDescriptor HighlightBorderOnMouseEvents;
  				HighlightBorderOnMouseEvents = (System.ComponentModel.PropertyDescriptor) properties["HighlightBorderOnMouseEvents"];
  				PropertyDescriptor HighlightBorderColor;
  				HighlightBorderColor = (System.ComponentModel.PropertyDescriptor) properties["HighlightBorderColor"];
  				PropertyDescriptor NormalBorderColor;
  				NormalBorderColor = (System.ComponentModel.PropertyDescriptor) properties["NormalBorderColor"];
  				PropertyDescriptor DropDownArrowBackColor;
  				DropDownArrowBackColor = (System.ComponentModel.PropertyDescriptor) properties["DropDownArrowBackColor"];
  				PropertyDescriptor tipoBordo;
  				tipoBordo = (System.ComponentModel.PropertyDescriptor) properties["BorderStyle"];
  				TipiBordi mtipoBordo;
	  			
  				mtipoBordo = ((TipiBordi) tipoBordo.GetValue(this.Component));
  				switch (mtipoBordo)
  				{
  					case TipiBordi.FlatXP:
	  					
  						properties["HighlightBorderOnMouseEvents"] = TypeDescriptor.CreateProperty(HighlightBorderOnMouseEvents.ComponentType, HighlightBorderOnMouseEvents, attributoFalso);
  						properties["HighlightBorderColor"] = TypeDescriptor.CreateProperty(HighlightBorderColor.ComponentType, HighlightBorderColor, attributoFalso);
  						properties["NormalBorderColor"] = TypeDescriptor.CreateProperty(NormalBorderColor.ComponentType, NormalBorderColor, attributoFalso);
  						properties["DropDownArrowBackColor"] = TypeDescriptor.CreateProperty(DropDownArrowBackColor.ComponentType, DropDownArrowBackColor, attributoFalso);
  						break;
  					case TipiBordi.Fixed3D:
	  					
  						properties["HighlightBorderOnMouseEvents"] = TypeDescriptor.CreateProperty(HighlightBorderOnMouseEvents.ComponentType, HighlightBorderOnMouseEvents, attributoVero);
  						properties["HighlightBorderColor"] = TypeDescriptor.CreateProperty(HighlightBorderColor.ComponentType, HighlightBorderColor, attributoVero);
  						properties["NormalBorderColor"] = TypeDescriptor.CreateProperty(NormalBorderColor.ComponentType, NormalBorderColor, attributoVero);
  						properties["DropDownArrowBackColor"] = TypeDescriptor.CreateProperty(DropDownArrowBackColor.ComponentType, DropDownArrowBackColor, attributoVero);
  						break;
  				}
	  			
  				PropertyDescriptor sourceDataTable;
  				sourceDataTable = (System.ComponentModel.PropertyDescriptor) properties["SourceDataTable"];
  				PropertyDescriptor sourceDataString;
  				sourceDataString = (System.ComponentModel.PropertyDescriptor) properties["SourceDataString"];
  				PropertyDescriptor tipoCaricamento;
  				tipoCaricamento = (System.ComponentModel.PropertyDescriptor) properties["LoadingType"];
  				CaricamentoCombo mtipoCaricamento;
	  			
  				mtipoCaricamento = ((CaricamentoCombo) tipoCaricamento.GetValue(this.Component));
  				switch (mtipoCaricamento)
  				{
  					case CaricamentoCombo.ComboBoxItem:
	  					
  						properties["SourceDataTable"] = TypeDescriptor.CreateProperty(sourceDataTable.ComponentType, sourceDataTable, attributoVero);
  						properties["SourceDataString"] = TypeDescriptor.CreateProperty(sourceDataString.ComponentType, sourceDataString, attributoVero);
  						break;
  					case CaricamentoCombo.DataTable:
	  					
  						properties["SourceDataTable"] = TypeDescriptor.CreateProperty(sourceDataTable.ComponentType, sourceDataTable, attributoFalso);
  						properties["SourceDataString"] = TypeDescriptor.CreateProperty(sourceDataString.ComponentType, sourceDataString, attributoFalso);
  						break;
  				}
	  			
  				PropertyDescriptor ManagingFastMouseMovingInterval;
  				ManagingFastMouseMovingInterval = (System.ComponentModel.PropertyDescriptor) properties["ManagingFastMouseMovingInterval"];
  				bool mManagingFastMouseMoving;
  				PropertyDescriptor ManagingFastMouseMoving;
  				ManagingFastMouseMoving = (System.ComponentModel.PropertyDescriptor) properties["ManagingFastMouseMoving"];
	  			
  				mManagingFastMouseMoving = System.Convert.ToBoolean(ManagingFastMouseMoving.GetValue(this.Component));
  				if (mManagingFastMouseMoving)
  				{
  					properties["ManagingFastMouseMovingInterval"] = TypeDescriptor.CreateProperty(ManagingFastMouseMovingInterval.ComponentType, ManagingFastMouseMovingInterval, attributoFalso);
  				}
  				else
  				{
  					properties["ManagingFastMouseMovingInterval"] = TypeDescriptor.CreateProperty(ManagingFastMouseMovingInterval.ComponentType, ManagingFastMouseMovingInterval, attributoVero);
  				}
  			}
	  		
  			//Starting values when you drop the control on a form
  			public override void OnSetComponentDefaults ()
  			{
  				((ComboBoxEx) this.Component).Text = "";
  				((ComboBoxEx) this.Component).BorderStyle = TipiBordi.FlatXP;
  			}
  		}

  		#endregion
	  	
  		#region " **************** Properties **************** "
  		[Description("When DropDownList, you can only select items in the combo")]
		public new CustomDropDownStyle DropDownStyle
  		{
  			get{
  				return m_DropDownStyle;
  			}
  			set
  			{
  				m_DropDownStyle = value;
  				TypeDescriptor.Refresh(this);
  			}
  		}
	  	
  		[Description("Set this property to \'True\' if you want to manage the fast mouse moving over the combo while Highlighted")]
		public bool ManagingFastMouseMoving
  		{
  			get{
  				return m_ManagingFastMouseMoving;
  			}
  			set
  			{
  				m_ManagingFastMouseMoving = value;
  				TypeDescriptor.Refresh(this);
  			}
  		}
	  	
  		[Description("Timer interval used in Fast Mouve Moving managament (in ms)")]
		public int ManagingFastMouseMovingInterval
  		{
  			get{
  				return m_ManagingFastMouseMovingInterval;
  			}
  			set
  			{
  				m_ManagingFastMouseMovingInterval = value;
  			}
  		}
	  	
	  	
  		[Description("Border Color when the Combobox is not Highlighted")]
		public Color NormalBorderColor
  		{
  			get{
  				return m_NormalBorderColor;
  			}
  			set
  			{
  				m_NormalBorderColor = value;
  				this.Invalidate();
  			}
  		}
	  	
  		[Description("Text Color of the item selected in the DropDownList")]
		public Color DropDownForeColor
  		{
  			get{
  				return m_DropDownForeColor;
  			}
  			set
  			{
  				m_DropDownForeColor = value;
  			}
  		}
	  	
  		[Description("Back Color of the item selected in the DropDownList")]
		public Color DropDownBackColor
  		{
  			get{
  				return m_DropDownBackColor;
  			}
  			set
  			{
  				m_DropDownBackColor = value;
  			}
  		}
	  	
  		[Description("Background Color of the Arrow when the Dropdownlist is open")]
		public Color DropDownArrowBackColor
  		{
  			get{
  				return m_DropDownArrowBackColor;
  			}
  			set
  			{
  				m_DropDownArrowBackColor = value;
  			}
  		}
	  	
  		[Description("Columns number (max 4)")]
		public int ColumnNum
  		{
  			get{
  				return m_ColumnNum;
  			}
  			set
  			{
  				if (value > 4)
  				{
  					m_ColumnNum = 4;
  				}
  				else if (value < 1)
  				{
  					m_ColumnNum = 1;
  				}
  				else
  				{
  					m_ColumnNum = value;
  				}
  			}
  		}
	  	
  		[Description("Size of columns in pixel, splitted by ;"), RefreshProperties(RefreshProperties.All)]
		public string ColumnWidth
  		{
  			get{
  				return m_ColumnWidth;
  			}
  			set
  			{
  				m_ColumnWidth = value;
  				switch (this.ColumnNum)
  				{
  					case 1:
	  					
  						wcol1 = m_ColumnWidth;
  						if (this.DropDownWidth < System.Convert.ToInt32(wcol1) + 20)
  						{
  							this.DropDownWidth = System.Convert.ToInt32(wcol1) + 20; //+20 to take care of vertical scrollbar
  						}
  						break;
  					case 2:
	  					
  						wcol = m_ColumnWidth;
  						wcol1 = Microsoft.VisualBasic.Strings.Left(wcol, Strings.InStr(wcol, ";", 0) - 1);
  						wcol2 = Microsoft.VisualBasic.Strings.Right(wcol, wcol.Length- wcol1.Length- 1);
  						if (this.DropDownWidth < System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + 20)
  						{
  							this.DropDownWidth = System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + 20; //+20 to take care of vertical scrollbar
  						}
  						break;
  					case 3:
	  					
  						wcol = m_ColumnWidth;
  						wcol1 = Microsoft.VisualBasic.Strings.Left(wcol, Strings.InStr(wcol, ";", 0) - 1);
  						wcol = Microsoft.VisualBasic.Strings.Right(wcol, wcol.Length- wcol1.Length- 1);
  						wcol2 = Microsoft.VisualBasic.Strings.Left(wcol, Strings.InStr(wcol, ";", 0) - 1);
  						wcol3 = Microsoft.VisualBasic.Strings.Right(wcol, wcol.Length- wcol2.Length- 1);
  						if (this.DropDownWidth < System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) + 20)
  						{
  							this.DropDownWidth = System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) + 20; //+20 to take care of vertical scrollbar
  						}
  						break;
  					case 4:
	  					
  						wcol = m_ColumnWidth;
						wcol1 = Microsoft.VisualBasic.Strings.Left(wcol, Strings.InStr(wcol, ";", 0) - 1);
  						wcol = Microsoft.VisualBasic.Strings.Right(wcol, wcol.Length- wcol1.Length- 1);
  						wcol2 = Microsoft.VisualBasic.Strings.Left(wcol, Strings.InStr(wcol, ";", 0) - 1);
  						wcol = Microsoft.VisualBasic.Strings.Right(wcol, wcol.Length- wcol2.Length- 1);
  						wcol3 = Microsoft.VisualBasic.Strings.Left(wcol, Strings.InStr(wcol, ";", 0) - 1);
  						wcol4 = Microsoft.VisualBasic.Strings.Right(wcol, wcol.Length- wcol3.Length- 1);
  						if (this.DropDownWidth < System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) + System.Convert.ToInt32(wcol4) + 20)
  						{
  							this.DropDownWidth = System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) + System.Convert.ToInt32(wcol4) + 20; //+20 to take care of vertical scrollbar
  						}
  						break;
  				}
  			}
  		}
	  	
  		[Description("Set to true if you want the vertical line to divide every column in the Dropdownlist")]
		public bool GridLineVertical
  		{
  			get{
  				return m_GridLineVertical;
  			}
  			set
  			{
  				m_GridLineVertical = value;
  			}
  		}
	  	
  		[Description("Set to true if you want the horizontal line to divide every column in the Dropdownlist")]
		public bool GridLineHorizontal
  		{
  			get{
  				return m_GridLineHorizontal;
  			}
  			set
  			{
  				m_GridLineHorizontal = value;
  			}
  		}
	  	
  		[Description("Color of the gridlines in the Dropdownlist")]
		public Color GridLineColor
  		{
  			get{
  				return m_GridLineColor;
  			}
  			set
  			{
  				m_GridLineColor = value;
  			}
  		}
	  	
  		[Description("Combobox text style: Normal, Upper, Lower")]
		public CharacterCasing CharacterCasing
  		{
  			get{
  				return m_CharacterCasing;
  			}
  			set
  			{
  				if (m_CharacterCasing != value)
  				{
  					m_CharacterCasing = value;
  					RecreateHandle();
  				}
  			}
  		}
  		protected override CreateParams CreateParams
  		{
  			get{
  				CreateParams cp = base.CreateParams;
  				if (m_CharacterCasing == CharacterCasing.Lower)
  				{
  					cp.Style = cp.Style | CBS_LOWERCASE;
  				}
  				else if (m_CharacterCasing == CharacterCasing.Upper)
  				{
  					cp.Style = cp.Style | CBS_UPPERCASE;
  				}
  				return cp;
  			}
  		}
	  	
  		[Description("Style of the Combobox Border")]
		public TipiBordi BorderStyle
  		{
  			get{
  				return m_BorderStyle;
  			}
  			set
  			{
  				m_BorderStyle = value;
  				if (value == TipiBordi.FlatXP)
  				{
  					this.HighlightBorderColor = Color.Blue;
  					this.HighlightBorderOnMouseEvents = true;
  					this.DropDownBackColor = Color.FromArgb(193, 210, 238);
  					this.DropDownArrowBackColor = Color.FromArgb(136, 169, 223);
  					this.DropDownForeColor = Color.Black;
  				}
  				this.Invalidate();
  				TypeDescriptor.Refresh(this);
  			}
  		}
	  	
  		[Description("How to load the combobox: through the ComboboxItem or a DataTable")]
		public CaricamentoCombo LoadingType
  		{
  			get{
  				return m_LoadingType;
  			}
  			set
  			{
  				m_LoadingType = value;
  				TypeDescriptor.Refresh(this);
  			}
  		}
	  	
  		[Description("Color of the Border when the combo is focused or the mouse is over")]
		public Color HighlightBorderColor
  		{
  			get{
  				return m_HighlightBorderColor;
  			}
  			set
  			{
  				m_HighlightBorderColor = value;
  			}
  		}
	  	
  		[Description("Set to true if you want to highlight the combobox on GotFocus and MouseEnter")]
		public bool HighlightBorderOnMouseEvents
  		{
  			get{
  				return m_HighlightBorderOnMouseEvents;
  			}
  			set
  			{
  				m_HighlightBorderOnMouseEvents = value;
  				TypeDescriptor.Refresh(this);
  			}
  		}
	  	
		[Description("DataTable used as source in the Combobox")]
		public DataTable SourceDataTable
		{
			get
			{
				return m_DataTable;
			}
			set
			{
				m_DataTable = value;
				if (value != null)
				{
					int j = 0;
					if (!(m_SourceDataString == null) &&(m_SourceDataString.Length > 0))
					{
						foreach (string Column_Name in m_SourceDataString)
						{
							if (m_DataTable.Columns.Contains(Column_Name))
							{
								int i = 0;
								foreach (DataColumn Colonna in m_DataTable.Columns)
								{
									if (Colonna.ColumnName.ToUpper()== Column_Name.ToUpper())
									{
										Indice[j] = i;
									}
									i += 1;
								}
								j += 1;
							}
						}
					}
					else
					{
						//the SourceDataString Property hasn't been set ---> columns are taken in the order they are in datatable
						Indice[0] = 0;
						Indice[1] = 1;
						Indice[2] = 2;
						Indice[3] = 3;
					}

					foreach (DataRow dr in value.Rows)
					{
						switch (this.ColumnNum)
						{
							case 1:
	  						
								this.Items.Add(new ComboBoxExItem(Assegna(dr[Indice[0]]), "", "", ""));
								break;
							case 2:
	  						
								this.Items.Add(new ComboBoxExItem(Assegna(dr[Indice[0]]), Assegna(dr[Indice[1]]), "", ""));
								break;
							case 3:
	  						
								this.Items.Add(new ComboBoxExItem(Assegna(dr[Indice[0]]), Assegna(dr[Indice[1]]), Assegna(dr[Indice[2]]), ""));
								break;
							case 4:
	  						
								this.Items.Add(new ComboBoxExItem(Assegna(dr[Indice[0]]), Assegna(dr[Indice[1]]), Assegna(dr[Indice[2]]), Assegna(dr[Indice[3]])));
								break;
						}
					}

				}
	  			
			}
		}

  		[Description("ColumnNames of the Datatable passed through SourceDataTable Property to show in the Dropdownlist")]
		public string[] SourceDataString
  		{
  			get{
  				return m_SourceDataString;
  			}
  			set
  			{
  				m_SourceDataString = value;
	  			
  				int j = 0;
  				if (m_DataTable != null)
  				{
  					foreach (string Column_Name in m_SourceDataString)
  					{
  						if (m_DataTable.Columns.Contains(Column_Name))
  						{
  							int i = 0;
  							foreach (DataColumn Colonna in m_DataTable.Columns)
  							{
  								if (Colonna.ColumnName.ToUpper()== Column_Name.ToUpper())
  								{
  									Indice[j] = i;
  								}
  								i += 1;
  							}
  							j += 1;
  						}
  					}
  				}
  			}
  		}
	  	

  		#endregion
	  	
  		#region " **************** General Methods and Overrides ****************"
  		//This function is used to take care of DBNull in the DataTable
  		private string Assegna(object Value)
  		{
  			string returnValue;
  			if (Information.IsDBNull(Value))
  			{
  				returnValue = "";
  			}
  			else
  			{
  				returnValue = Value.ToString();
  			}
  			return returnValue;
  		}
	  	
  		//Event fired every time the Timer ticks
  		private void TimerEventProcessor (object myObject, EventArgs myEventArgs)
  		{
  			if (this.BorderStyle == TipiBordi.FlatXP && this.DesignMode == false)
  			{
  				if (this.Focused)
  				{
  					return;
  				}
  				bool mouseIsOver;
  				Point mousePosition = Control.MousePosition;
  				try
  				{
  					mousePosition = PointToClient(mousePosition);
  				}
  				catch (Exception)
  				{
  					//Debug.WriteLine("Error")
  					return;
  				}
  				mouseIsOver = ClientRectangle.Contains(mousePosition);
  				if (currentColor.Equals(m_HighlightBorderColor))
  				{
  					//Combo active
  					if (! mouseIsOver)
  					{
  						Graphics g = Graphics.FromHwnd(this.Handle);
  						DrawBorder(g, m_NormalBorderColor);
  						DrawNormalArrow(ref g, true);
  						currentColor = m_NormalBorderColor;
  						g.Dispose();
  						MouseOver = false;
  					}
  				}
  				else if (currentColor.Equals(m_NormalBorderColor))
  				{
  					//Combo disactive
  					if (mouseIsOver && MouseOver)
  					{
  						Graphics g = Graphics.FromHwnd(this.Handle);
  						if (this.HighlightBorderOnMouseEvents == true)
  						{
  							if (! Highlighted)
  							{
  								currentColor = this.HighlightBorderColor;
  								DrawBorder(g, currentColor);
  								DrawHighlightedArrow(ref g, false);
  							}
  						}
  						g.Dispose();
  					}
  				}
  			}
  		}
	  	
  		//Calculate the location of the Arrow Box
  		private void ArrowBoxPosition (ref int left, ref int top, ref int width, ref int height)
  		{
  			Rectangle rc = ClientRectangle;
  			width = arrowWidth;
  			left = rc.Right - width - 2;
  			top = rc.Top + 2;
  			height = rc.Height - 4;
  		}
	  	
  		//Draw the Flat Arrow Box when not highlighted
  		private void DrawNormalArrow (ref Graphics g, bool disable)
  		{
  			if (this.BorderStyle == TipiBordi.FlatXP)
  			{
  				int left = 0;
  				int top = 0;
  				int arrowWidth = 0;
  				int height = 0;

  				ArrowBoxPosition(ref left, ref top, ref arrowWidth, ref height);
	  			
  				Brush stripeColorBrush = new SolidBrush(SystemColors.Control);
  				int Larghezza = SystemInformation.VerticalScrollBarWidth - arrowWidth;
  				if (this.Enabled)
  				{
  					Brush b = new SolidBrush(SystemColors.Control);
  					g.FillRectangle(b, new Rectangle(left - Larghezza, top - 2, SystemInformation.VerticalScrollBarWidth, height + 4));
  				}
	  			
  				if (this.Enabled)
  				{
  					Pen p = new Pen(m_NormalBorderColor);
  					g.DrawLine(p, new Point(ClientRectangle.Right - SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Top), new Point(ClientRectangle.Right, ClientRectangle.Top));
  					g.DrawLine(p, new Point(ClientRectangle.Right - SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Bottom - 1), new Point(ClientRectangle.Right, ClientRectangle.Bottom - 1));
	  				
  					if (! disable)
  					{
  						System.Drawing.Graphics temp_SystemDrawingGraphics = g;
  						DrawHighlightedArrow(ref temp_SystemDrawingGraphics, true);
  						g.FillRectangle(stripeColorBrush, left, top - 1, arrowWidth + 1, height + 2);
  					}
  					else
  					{
  						g.FillRectangle(stripeColorBrush, left - 4, top - 1, arrowWidth + 5, height + 2);
  					}
	  				
  					DrawArrow(g, false);
  				}
  				else
  				{
  					Pen p = new Pen(SystemColors.InactiveBorder);
  					g.DrawLine(p, new Point(ClientRectangle.Right - SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Top), new Point(ClientRectangle.Right, ClientRectangle.Top));
  					g.DrawLine(p, new Point(ClientRectangle.Right - SystemInformation.VerticalScrollBarWidth - 2, ClientRectangle.Bottom - 1), new Point(ClientRectangle.Right, ClientRectangle.Bottom - 1));
	  				
  					// Now draw the unselected background
  					g.FillRectangle(stripeColorBrush, left - 5, top - 1, arrowWidth + 6, height + 2);
	  				
  					DrawArrow(g, true);
  				}
  				Highlighted = false;
  			}
  		}
	  	
  		//Draw the Flat Arrow Box when highlighted
  		private void DrawHighlightedArrow (ref Graphics g, bool Delete)
  		{
  			if (this.BorderStyle == TipiBordi.FlatXP)
  			{
  				int left = 0;
  				int top = 0;
  				int arrowWidth = 0;
  				int height = 0;

  				ArrowBoxPosition(ref left, ref top, ref arrowWidth, ref height);
	  			
  				if (this.Enabled)
  				{
  					int comboTextWidth = SystemInformation.VerticalScrollBarWidth - arrowWidth;
  					if (comboTextWidth < 0)
  					{
  						comboTextWidth = 1;
  					}
  					Brush b = new SolidBrush(HighlightBorderColor);
  				}
	  			
  				if (! Delete)
  				{
  					if (DroppedDown)
  					{
  						Graphics cbg = CreateGraphics();
  						Brush pressedColorBrush = new SolidBrush(m_DropDownArrowBackColor);
  						int Larghezza = SystemInformation.VerticalScrollBarWidth - arrowWidth;
  						cbg.FillRectangle(pressedColorBrush, new Rectangle((left - Larghezza), top - 1, SystemInformation.VerticalScrollBarWidth + 1, height + 2));
  						Pen p = new Pen(HighlightBorderColor);
  						cbg.DrawRectangle(p,(left - Larghezza) - 1, top - 2, SystemInformation.VerticalScrollBarWidth + 2, height + 4);
  						DrawArrow(cbg, false);
  						cbg.Dispose();
  						return;
  					}
  					else
  					{
  						if (Enabled)
  						{
  							Brush b = new SolidBrush(m_DropDownBackColor);
  							int Larghezza = SystemInformation.VerticalScrollBarWidth - arrowWidth;
  							g.FillRectangle(b, new Rectangle((left - Larghezza), top - 1, SystemInformation.VerticalScrollBarWidth + 1, height + 2));
	  						
  							Color pencolor = customBorderColor;
  							if (pencolor.Equals(Color.Empty))
  							{
  								pencolor = BackColor;
  							}
  						}
  					}
  				}
  				else
  				{
  					Brush b = new SolidBrush(BackColor);
  					g.FillRectangle(b, left - 1, top - 1, arrowWidth + 2, height + 2);
  				}
  				if (this.Enabled)
  				{
  					DrawArrow(g, false);
  				}
  				Highlighted = true;
  			}
  		}
	  	
  		private void DrawArrow (Graphics g, bool Disable)
  		{
  			if (this.BorderStyle == TipiBordi.FlatXP)
  			{
  				int left = 0;
  				int top = 0;
  				int arrowWidth = 0;
  				int height = 0;

  				ArrowBoxPosition(ref left, ref top, ref arrowWidth, ref height);
	  			
  				int extra = 1;
  				if (bUsingLargeFont)
  				{
  					extra = 2;
  				}
	  			
  				//triangle vertex of the arrow
  				Point[] pts = new Point[3];
  				pts[0] = new Point(left + arrowWidth / 2 - 2 - extra - 2, top + height / 2 - 1);
  				pts[1] = new Point(left + arrowWidth / 2 + 3 + extra - 1, top + height / 2 - 1);
  				pts[2] = new Point(left + arrowWidth / 2 - 1,(top + height / 2 - 1) + 3 + extra);
	  			
  				//draw the arrow as a polygon
  				if (Disable)
  				{
  					Brush b = new SolidBrush(arrowDisableColor);
  					g.FillPolygon(b, pts);
  				}
  				else
  				{
  					Brush b = new SolidBrush(arrowColor);
  					g.FillPolygon(b, pts);
  				}
  			}
  		}
	  	
  		private void DrawBorder (Graphics g, Color DrawColor)
  		{
  			if (this.BorderStyle == TipiBordi.FlatXP)
  			{
  				g.DrawRectangle(new Pen(this.BackColor, 1), ClientRectangle.Left + 1, ClientRectangle.Top + 1, ClientRectangle.Width - 1, ClientRectangle.Height - 3);
	  			
  				//Draw the Border
  				if (this.Enabled == false) //combo disabilitato
  				{
  					DrawColor = SystemColors.InactiveBorder;
  				}
	  			
  				Pen pen = new Pen(DrawColor, 1);
  				//Border Rectangle
  				g.DrawRectangle(pen, ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
  				//Button Rectangle
  				g.DrawRectangle(pen, ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth - 3, ClientRectangle.Height - 1);
  			}
  		}
	  	
  		protected override void WndProc (ref Message m)
  		{
			base.WndProc(ref m);
			bool mouseIsOver=false;
			Point mousePosition;

  			if (this.BorderStyle == TipiBordi.FlatXP)
  			{
				switch (m.Msg)
  				{
  					case 0xF:
  						//WM_PAINT
	  					
  						//We have to find if the Mouse is Over the combo
  						mousePosition = Control.MousePosition;
  						mousePosition = PointToClient(mousePosition);
  						mouseIsOver = ClientRectangle.Contains(mousePosition);
	  					
  						if (this.HighlightBorderOnMouseEvents &&(mouseIsOver || this.Focused))
  						{
  							Graphics g = Graphics.FromHwnd(this.Handle);
	  						
  							DrawBorder(g, this.HighlightBorderColor);
  							System.Drawing.Graphics temp_SystemDrawingGraphics = g;
  							DrawHighlightedArrow(ref temp_SystemDrawingGraphics, false);
  						}
  						else
  						{
  							Graphics g = Graphics.FromHwnd(this.Handle);
	  						
  							DrawBorder(g, m_NormalBorderColor);
  							System.Drawing.Graphics temp_SystemDrawingGraphics2 = g;
  							DrawNormalArrow(ref temp_SystemDrawingGraphics2, true);
  						}
  						break;
	  					
	  					
  					case 0x133:
	  					
  						//WM_PAINT
	  					
  						//We have to find if the Mouse is Over the combo
  						mousePosition = Control.MousePosition;
  						mousePosition = PointToClient(mousePosition);
  						mouseIsOver = ClientRectangle.Contains(mousePosition);
	  					
  						if (this.HighlightBorderOnMouseEvents &&(mouseIsOver || this.Focused))
  						{
  							Graphics g = Graphics.FromHwnd(this.Handle);
	  						
  							DrawBorder(g, this.HighlightBorderColor);
  							DrawHighlightedArrow(ref g, false);
  						}
  						else
  						{
  							Graphics g = Graphics.FromHwnd(this.Handle);
	  						
  							DrawBorder(g, m_NormalBorderColor);
  							DrawNormalArrow(ref g, true);
  						}
  						break;
	  					
  					case 0x2A3:
	  					
  						//WM_MOUSELEAVE
  						if (this.Focused)
  						{
  							return;
  						}
  						if (currentColor.Equals(m_HighlightBorderColor))
  						{
  							mousePosition = Control.MousePosition;
  							mousePosition = PointToClient(mousePosition);
  							mouseIsOver = ClientRectangle.Contains(mousePosition);
	  						
  							if (! mouseIsOver)
  							{
  								Graphics g = Graphics.FromHwnd(this.Handle);
  								DrawBorder(g, m_NormalBorderColor);
  								DrawNormalArrow(ref g, true);
  								g.Dispose();
  							}
  						}
  						break;
  					case 0x200:
	  					
  						//WM_MOUSEMOVE
  						if (this.HighlightBorderOnMouseEvents == true && ! Highlighted)
  						{
  							currentColor = this.HighlightBorderColor;
  							Graphics g = Graphics.FromHwnd(this.Handle);
  							DrawBorder(g, currentColor);
  							DrawHighlightedArrow(ref g, false);
  							g.Dispose();
  						}
  						break;
  					case 0x46:
	  					
  						//WM_WINDOWPOSCHANGING
  						if (this.BorderStyle == TipiBordi.FlatXP)
  						{
  							//Repaint the arrow when pressed
  							if (this.HighlightBorderOnMouseEvents)
  							{
  								Graphics g = Graphics.FromHwnd(this.Handle);
  								Brush pressedColorBrush = new SolidBrush(m_DropDownBackColor);
  								int Larghezza = SystemInformation.VerticalScrollBarWidth - arrowWidth;
  								g.FillRectangle(pressedColorBrush, new Rectangle((Left - Larghezza), Top - 1, SystemInformation.VerticalScrollBarWidth + 1, Height + 2));
  								Pen p = new Pen(HighlightBorderColor);
  								g.DrawRectangle(p,(Left - Larghezza) - 1, Top - 2, SystemInformation.VerticalScrollBarWidth + 2, Height + 4);
  								DrawArrow(g, false);
  								g.Dispose();
  								this.Invalidate();
  							}
  						}
  						break;
  					default:
	  					
  						break;
	  					
  				}
  			}
  		}
	  	
  		//Custom painting of the DropDownList
  		private void mComboBox_DrawItem (object sender, System.Windows.Forms.DrawItemEventArgs e)
  		{
            Control otrol = sender as Control;
            Graphics g = e.Graphics;
            Rectangle r = e.Bounds;
            SolidBrush b;
            if (e.Index >= 0)
            {
                Rectangle rd = r;
                rd.Width = rd.Left + 100;
                b = new SolidBrush(otrol.ForeColor);
                g.FillRectangle(b, rd);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                //******************* WINDOWS 98 **********************
                if (e.State == DrawItemState.Selected)
                {
                    //item selected
                    e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), r);
                    switch (this.ColumnNum)
                    {
                        case 1:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                if (this.m_GridLineHorizontal)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                                }
                            }
                            break;
                        case 2:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                        case 3:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol3) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col3.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[2]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                        case 4:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol3) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col3.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[2]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol4) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) - System.Convert.ToInt32(wcol3) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col4.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[3]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                    }
                    if (this.BorderStyle == TipiBordi.FlatXP)
                    {
                        //Use the border color to highlight the selected item
                        if (this.GridLineHorizontal)
                        {
                            e.Graphics.DrawRectangle(new Pen(this.HighlightBorderColor, 1), r.X, r.Y, r.Width - 1, r.Height - 2);
                        }
                        else
                        {
                            e.Graphics.DrawRectangle(new Pen(this.HighlightBorderColor, 1), r.X, r.Y, r.Width - 1, r.Height - 1);
                        }
                    }
                    e.DrawFocusRectangle();

                    //******************* WINDOWS 2000/XP **********************
                }
                else if ((e.State == (DrawItemState.NoAccelerator | DrawItemState.Selected)) || (e.State == (DrawItemState.Selected | DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)))
                {
                    //item selected
                    e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), r);
                    switch (this.ColumnNum)
                    {
                        case 1:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {

                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                if (this.m_GridLineHorizontal)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                                }
                            }
                            break;
                        case 2:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                        case 3:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol3) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[2]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col3.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                        case 4:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol3) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[2]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col3.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol4) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(DropDownBackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) - System.Convert.ToInt32(wcol3) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[3]]).ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col4.ToString(), this.Font, new SolidBrush(DropDownForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                    }
                    if (this.BorderStyle == TipiBordi.FlatXP)
                    {
                        //Use the border color to highlight the selected item
                        if (this.GridLineHorizontal)
                        {
                            e.Graphics.DrawRectangle(new Pen(this.HighlightBorderColor, 1), r.X, r.Y, r.Width - 1, r.Height - 2);
                        }
                        else
                        {
                            e.Graphics.DrawRectangle(new Pen(this.HighlightBorderColor, 1), r.X, r.Y, r.Width - 1, r.Height - 1);
                        }
                    }
                    e.DrawFocusRectangle();
                }
                else
                {
                    //items NOT selected
                    e.Graphics.FillRectangle(new SolidBrush(otrol.BackColor), r);
                    if (BorderStyle == TipiBordi.FlatXP)
                    {
                        if (this.GridLineHorizontal)
                        {
                            e.Graphics.DrawRectangle(new Pen(this.BackColor, 1), r.X, r.Y, r.Width, r.Height - 1);
                        }
                        else
                        {
                            e.Graphics.DrawRectangle(new Pen(this.BackColor, 1), r.X, r.Y, r.Width, r.Height);
                        }
                    }
                    switch (this.ColumnNum)
                    {
                        case 1:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                        case 2:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(otrol.BackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                        case 3:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(otrol.BackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol3) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(otrol.BackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[2]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col3.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                        case 4:

                            if (System.Convert.ToInt32(wcol1) > 0)
                            {
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[0]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col1.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X, rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol2) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(otrol.BackColor), rd.X + System.Convert.ToInt32(wcol1) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[1]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col2.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol3) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(otrol.BackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[2]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {
                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col3.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2), rd.Y, sf);
                                }
                            }
                            if (System.Convert.ToInt32(wcol4) > 0)
                            {
                                if (this.m_GridLineVertical)
                                {
                                    e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 2, rd.Y, rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 2, rd.Y + 15);
                                }
                                e.Graphics.FillRectangle(new SolidBrush(otrol.BackColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3) - 1, rd.Y, r.Width - System.Convert.ToInt32(wcol1) - System.Convert.ToInt32(wcol2) - System.Convert.ToInt32(wcol3) + 1, r.Height);
                                if (this.LoadingType == CaricamentoCombo.DataTable)
                                {
                                    e.Graphics.DrawString(Assegna(m_DataTable.Rows[e.Index][Indice[3]]).ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3), rd.Y, sf);
                                }
                                else if (this.LoadingType == CaricamentoCombo.ComboBoxItem)
                                {

                                    e.Graphics.DrawString(((ComboBoxExItem)this.Items[e.Index]).Col4.ToString(), this.Font, new SolidBrush(otrol.ForeColor), rd.X + System.Convert.ToInt32(wcol1) + System.Convert.ToInt32(wcol2) + System.Convert.ToInt32(wcol3), rd.Y, sf);
                                }
                            }
                            if (this.m_GridLineHorizontal)
                            {
                                e.Graphics.DrawLine(new Pen(GridLineColor, 1), rd.X, rd.Y + rd.Height - 1, rd.X + this.DropDownWidth, rd.Y + rd.Height - 1);
                            }
                            break;
                    }
                    e.DrawFocusRectangle();
                }
            }
  		}
	  	
  		protected override void OnKeyPress (System.Windows.Forms.KeyPressEventArgs e)
  		{
  			//AUTOCOMPLETE: we have to know when a key has been really pressed
	  		
  			if (this.DropDownStyle == CustomDropDownStyle.DropDown)
  			{
  				PressedKey = true;
  			}
  			else
  			{
  				//ReadOnly AutoComplete Management
  				string sTypedText;
  				int iFoundIndex;
  				string currentText;
  				int Start;
  				int selLength;
	  			
  				if (Strings.Asc(e.KeyChar) == 8)
  				{
  					if (this.SelectedText == this.Text)
  					{
  						PressedKey = true;
  						return;
  					}
  				}
  				if (this.SelectionLength > 0)
  				{
  					Start = this.SelectionStart;
  					selLength = this.SelectionLength;
	  				
  					//This is equivalent to Me.Text, but sometimes using Me.Text it doesn't work
  					currentText = this.AccessibilityObject.Value;
	  				
  					currentText = currentText.Remove(Start, selLength);
  					currentText = currentText.Insert(Start, e.KeyChar.ToString());
  					sTypedText = currentText;
  				}
  				else
  				{
  					Start = this.SelectionStart;
  					sTypedText = this.Text.Insert(Start, e.KeyChar.ToString());
  				}
  				iFoundIndex = this.FindString(sTypedText);
  				if (iFoundIndex >= 0)
  				{
  					PressedKey = true;
  				}
  				else
  				{
  					e.Handled = true;
  				}
  			}
  		}
	  	
  		protected override void OnKeyDown (System.Windows.Forms.KeyEventArgs e)
  		{
  			if (this.DropDownStyle == CustomDropDownStyle.DropDownList && e.KeyCode == Keys.Delete)
  			{
  				if (this.Text != this.SelectedText)
  				{
  					e.Handled = true;
  				}
  			}
	  		
  			base.OnKeyDown(e);
  		}
	  	
  		protected override void OnKeyUp (System.Windows.Forms.KeyEventArgs e)
  		{
  			//AUTOCOMPLETING
	  		
  			//WARNING: With VB.Net 2003 there is a strange behaviour. This event is raised not just when any key is pressed
  			//but also when the Me.Text property changes. Particularly, it happens when you write in a fast way (for example you
  			//you press 2 keys and the event is raised 3 times). To manage this we have added a boolean variable PressedKey that
  			//is set to true in the OnKeyPress Event
	  		
  			string sTypedText;
  			int iFoundIndex;
  			object oFoundItem;
  			string sFoundText;
  			string sAppendText;
	  		
  			if (PressedKey)
  			{
  				//Ignoring alphanumeric chars
  				switch (e.KeyCode)
  				{
  					case Keys.Back:
  						return;
	  					
  					case Keys.Left:
  						return;
	  					
  					case Keys.Right:
  						return;
	  					
  					case Keys.Up:
  						return;
	  					
  					case Keys.Delete:
  						return;
	  					
  					case Keys.Down:
  						return;
	  					
  					case Keys.End:
  						return;
	  					
  					case Keys.Home:
	  					
  						return;
  				}
	  			
  				//Get the Typed Text and Find it in the list
  				sTypedText = this.Text;
  				iFoundIndex = this.FindString(sTypedText);
	  			
  				//If we found the Typed Text in the list then Autocomplete
  				if (iFoundIndex >= 0)
  				{
	  				
  					//Get the Item from the list (Return Type depends if Datasource was bound
  					// or List Created)
  					oFoundItem = this.Items[iFoundIndex];
	  				
  					//Use the ListControl.GetItemText to resolve the Name in case the Combo
  					// was Data bound
  					sFoundText = this.GetItemText(oFoundItem);
	  				
  					//Append then found text to the typed text to preserve case
  					sAppendText = sFoundText.Substring(sTypedText.Length);
  					this.Text = sTypedText + sAppendText;
	  				
  					//Select the Appended Text
  					this.SelectionStart = sTypedText.Length;
  					this.SelectionLength = sAppendText.Length;
	  				
  					if (e.KeyCode == Keys.Enter)
  					{
  						iFoundIndex = this.FindStringExact(this.Text);
  						this.SelectedIndex = iFoundIndex;
  						SendKeys.Send("\t");
  						e.Handled = true;
  					}
  				}
	  			
  			}
  			PressedKey = false;
  		}
	  	
  		protected override void OnLeave (System.EventArgs e)
  		{
  			//Selecting the item which text is showed in the text area of the ComboBox
  			int iFoundIndex;
  			//The Me.AccessibilityObject.Value is used instead of Me.Text to manage
  			//the event when you write in the combobox text and the DropDownList
  			//is open. In this case, if you click outside the combo, Me.Text mantains
  			//the old value and not the current one
            try
            {
                iFoundIndex = this.FindStringExact(this.AccessibilityObject.Value);
                this.SelectedIndex = iFoundIndex;
            }
            catch
            {
                return;
            }
  		}
	  	
  		protected override void OnCreateControl ()
  		{
  			this.DisplayMember = "Text";
  			this.DrawMode = DrawMode.OwnerDrawFixed;
  			currentColor = m_NormalBorderColor;
  			this.Invalidate();
  		}
	  	
  		protected override void OnHandleCreated (System.EventArgs e)
  		{
  			base.OnHandleCreated(e);
  			if (this.ManagingFastMouseMoving)
  			{
  				myTimer.Interval = m_ManagingFastMouseMovingInterval;
  				myTimer.Start();
  			}
  		}
	  	
  		protected override void OnEnabledChanged (System.EventArgs e)
  		{
  			base.OnEnabledChanged(e);
  			Message m = new Message();
  			m.Msg = 0xF;
  			if (this.Enabled)
  			{
  				currentColor = this.NormalBorderColor;
  			}
  			else
  			{
  				currentColor = SystemColors.InactiveBorder;
  			}
  			//Generate a PAINT event
  			WndProc(ref m);
  		}
	  	
  		#endregion
	  	
  		#region " **************** Mouse and focus Overrides ****************"
  		//Override mouse and focus events to draw
  		//proper borders. Basically, set the color and Invalidate(),
  		//In general, Invalidate causes a control to redraw itself.
  		protected override void OnMouseEnter (System.EventArgs e)
  		{
  			base.OnMouseEnter(e);
  			if (this.HighlightBorderOnMouseEvents == true && ! Highlighted)
  			{
  				currentColor = this.HighlightBorderColor;
  				Graphics g = Graphics.FromHwnd(this.Handle);
  				DrawBorder(g, currentColor);
  				DrawHighlightedArrow(ref g, false);
  				g.Dispose();
  			}
  			MouseOver = true;
  		}
	  	
  		protected override void OnMouseHover (System.EventArgs e)
  		{
  			base.OnMouseHover(e);
  			MouseOver = true;
  		}
	  	
  		protected override void OnMouseLeave (System.EventArgs e)
  		{
  			base.OnMouseLeave(e);
  			if (this.Focused)
  			{
  				return;
  			}
  			if (this.HighlightBorderOnMouseEvents == true && Highlighted)
  			{
  				Graphics g = Graphics.FromHwnd(this.Handle);
  				DrawBorder(g, m_NormalBorderColor);
  				DrawNormalArrow(ref g, true);
  				g.Dispose();
  			}
  			MouseOver = false;
  		}
	  	
  		protected override void OnMouseMove (System.Windows.Forms.MouseEventArgs e)
  		{
  			base.OnMouseMove(e);
  			MouseOver = true;
  		}
	  	
  		protected override void OnLostFocus (System.EventArgs e)
  		{
  			base.OnLostFocus(e);
  			if (this.HighlightBorderOnMouseEvents == true && Highlighted)
  			{
  				currentColor = this.NormalBorderColor;
  				Graphics g = Graphics.FromHwnd(this.Handle);
  				DrawBorder(g, m_NormalBorderColor);
  				DrawNormalArrow(ref g, true);
  				g.Dispose();
  			}
  		}
	  	
  		protected override void OnGotFocus (System.EventArgs e)
  		{
  			base.OnGotFocus(e);
  			if (this.HighlightBorderOnMouseEvents == true && ! Highlighted)
  			{
  				currentColor = this.HighlightBorderColor;
  				Graphics g = Graphics.FromHwnd(this.Handle);
  				DrawBorder(g, currentColor);
  				DrawHighlightedArrow(ref g, false);
  				g.Dispose();
  			}
  		}
  		#endregion
	  	
	  
	  
	#region " ----- This class is used to create and manage the items of the combobox (with max 4 columns) -----"
	  
	public class ComboBoxExItem : ListViewItem, IComparable
	{
	  	
  		//Since all we need is a "Text" property for this example we can
  		//Subclass by inheriting any object desired.
  		//For this example, we'll use the ListViewItem
	  	
  		//each of the below public declarations will be "visible" to the outside
  		//You may add as many of these declarations using whatever types you desire
  		public string Col1;
  		public string Col2;
  		public string Col3;
  		public string Col4;
	  	
  		//every value of MyInfo you want to store, get's added to the NEW declaration
  		public ComboBoxExItem(string C1, string C2, string C3, string C4) {
  			//transfer all incoming parameters to your local storage
  			Col1 = C1;
  			Col2 = C2;
  			Col3 = C3;
  			Col4 = C4;
  			//and finally, pass back the Text property
  			this.Text = C1;
  		}
	  	
  		//Function used to sort the items on first element Col1
  		public int CompareTo(object obj)
  		{
  			//every not nothing object is greater than nothing
  			if (obj == null)
  			{
  				return 1;
  			}
	  		
  			//this is used to take care of late binding
  			ComboBoxExItem other = ((ComboBoxExItem) obj);
	  		
  			//comparing strings
  			return Strings.StrComp(Col1, other.Col1, CompareMethod.Text);
  		}
	  	
	}
	  
	#endregion

	}
}
